package com.ys.openapi.organization.department;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.erp.utils.HttpUtils;
import com.erp.utils.PropertiesUtils;
import com.ys.openapi.organization.entity.DepartmentEntity;
import com.ys.openapi.utils.ClientUtils;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 云枢组织机构
 */
public class Department {
    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
    private ClientUtils utils=new ClientUtils();


    /**
     * 新增部门
     * @return
     */
    public Object addDepartment(DepartmentEntity departmentEntity) throws Exception{
        //根据上级部门名称查询上级部门信息
        String pname=departmentEntity.getParentId();
        Map<String, Object> map = searchDepartmentByDepartmentName(pname,true);
        departmentEntity.setParentId(map.get(pname).toString());
        departmentEntity.setQueryCode(map.get("queryCode")+"#"+departmentEntity.getSourceid());

        Object post = utils.post(PropertiesUtils.loadProperties("openapi").getProperty("addDepartment"), null,JSON.toJSONString(departmentEntity));
        return post;
    }
    //更新部门
    public Object updateDepartment(DepartmentEntity departmentEntity) throws Exception{
        //
        String oldname=departmentEntity.getParentId();
        Map<String, Object> map = searchDepartmentByDepartmentName(oldname,true);
        JSONObject jb=(JSONObject)map.get("group");
        departmentEntity.setId(map.get(oldname).toString());
        departmentEntity.setParentId(jb.getString("parentId"));
        departmentEntity.setSourceid(jb.getString("sourceId"));
        departmentEntity.setQueryCode(map.get("queryCode")+"#"+departmentEntity.getSourceid());


        Object post =utils.post(PropertiesUtils.loadProperties("openapi").getProperty("updateDepartment"), null,JSON.toJSONString(departmentEntity));
        return post;
    }
    //#删除部门
    public Object removeDepartment(DepartmentEntity departmentEntity) throws Exception{

       // Object post =utils.post(PropertiesUtils.loadProperties("openapi").getProperty("removeDepartment"), null,JSON.toJSONString(queryParams));

        return  this.updateDepartment(departmentEntity);
    }
    //根据id获取部门信息
    public  Object getDepartment(String id)throws Exception{

        Object get=utils.get(PropertiesUtils.loadProperties("openapi").getProperty("getDepartment")+"?id="+id );
        return get;
    }
    //#根据id查询其下部门 get方式
    public Object getDepartmentsByParentId(String id)throws Exception{
        Object get=utils.get(PropertiesUtils.loadProperties("openapi").getProperty("getDepartmentsByParentId")+"?parentId="+id+"&isDeleted=true" );
        return get;
    }
    //根据部门名称模糊查询部门
    public Map<String,Object> searchDepartmentByDepartmentName(String departmentName,boolean tf)throws Exception{
        Object get=utils.get(PropertiesUtils.loadProperties("openapi").getProperty("searchDepartmentByDepartmentName")+"?departmentName="+departmentName+"&isDeleted=true" );
        JSONArray datas =  (JSONArray) get ;
        Map<String,Object>map=new HashMap<>();
        for(int i=0;i< datas.size();i++){
            JSONObject jb=datas.getJSONObject(i);
            if(tf){
                if(departmentName.equals(jb.getString("name"))){
                    map.put(jb.getString("name"),jb.getString("id"));
                    map.put("group",jb);
                    return map;
                }
            }else {
                map.put(jb.getString("name"),jb.getString("id"));
            }

        }
        map.put("group",datas);

        return map;
    }
    public static void main(String[] args)throws Exception {
       // 2c2c80867a1f88d8017ab347f25e24ab测试1
        //2c2c80867a1f88d8017a58572f9309fd 集团
         /* {"parentId":"2c2c80867a1f88d8017ab347f25e24ab测试1","name":"测试1222","deptType":"YUNS_DEPT",
            "queryCode": "1_1#CP210717444444444444",
            "sourceid":"CP210717444444444444",
            "corpId": "main",
            "leaf": true,
            "scopeModel":{"deptVisibleType":"ALL_VISIBLE","deptVisibleScope":"[]","staffVisibleType":"ALL_VISIBLE",
            "staffVisibleScope":"[]"}}*/
       /* {"parentId":"2c2c80867a1f88d8017a58572f9309fd","name":"测试333","deptType":"YUNS_DEPT",
                "queryCode": "1_1#CP210717444444444444 ",
                "sourceid":"CP210717777",
                "corpId": "main",
                "leaf": true}*/
        //增加
        SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
        DepartmentEntity de=new DepartmentEntity();
        de.setCorpId("main");
        de.setDeptType("YUNS_DEPT");
        de.setLeaf(true);
        de.setName("测试33789");
        de.setSourceid("CP"+sdf.format(new Date())+ Calendar.getInstance().get(Calendar.MILLISECOND));
        de.setQueryCode("1_1#"+de.getSourceid());
        de.setParentId("2c2c80867a1f88d8017a58572f9309fd");
        de.setDeleted(false);
         //更新
       /* de.setId("2c2c80867ab40ee9017ac9e922010600");
        de.setSourceid("CP210710102422944");
        de.setQueryCode("1_1#CP210710102422944");*/
        //删除
       /* de.setDeleted(true);
        de.setId("2c2c80867ab40ee9017aca04ff1d0633");
        de.setSourceid("");
        de.setQueryCode("");*/


        String s = JSON.toJSONString(de);
        System.out.println(s);

        Department d=new Department();
      // d.addDepartment(de);
       // d.updateDepartment(de);
       // d.removeDepartment(de);
       // d.getDepartmentsByParentId("2c2c80867a1f88d8017a58572f9309fd");
       // d.getDepartment("2c2c80867a1f88d8017a58572f9309fd");
        d.searchDepartmentByDepartmentName("测试33789",true);
    }
}
